package org.example.market.service;

import org.example.market.entity.Provider;
import org.example.market.entity.Role;
import org.example.market.mapper.RoleMapper;
import org.example.market.util.PageResult;
import org.example.market.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;

    public Result<List<Role>> getAllRoles() {
        try {
            List<Role> roles = roleMapper.listAllRoles();
            return Result.success(roles);
        } catch (Exception e) {
            return Result.error(500, "获取角色列表失败: " + e.getMessage());
        }
    }

    public Result<Role> addRole(Role role) {
        try {
            roleMapper.insertRole(role);
            return Result.success(role);
        } catch (Exception e) {
            return Result.error(500, "添加角色失败: " + e.getMessage());
        }
    }

    public Result<Role> getRoleById(Long id) {
        try {
            Role role = roleMapper.getRoleById(id);
            if (role != null) {
                return Result.success(role);
            }
            return Result.error(404, "未找到该角色");
        } catch (Exception e) {
            return Result.error(500, "获取角色详情失败: " + e.getMessage());
        }
    }

    public Result<Role> updateRole(Role role) {
        try {
            roleMapper.updateRole(role);
            return Result.success(role);
        } catch (Exception e) {
            return Result.error(500, "更新角色失败: " + e.getMessage());
        }
    }

    public Result<Void> deleteRole(Long id) {
        try {
            roleMapper.deleteRoleById(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error(500, "删除角色失败: " + e.getMessage());
        }
    }



}